//
//  JMCenterManager.h
//  OpenSesame
//
//  Created by zhangjiaming on 17/5/5.
//  Copyright © 2016年 OpenSesame. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@class JMCenterManager;
@protocol JMCenterManagerDelegate <NSObject>
@optional
/** 检测手机蓝牙状态 */
- (void)centerManager:(JMCenterManager *)centerManager didUpdateState:(CBCentralManager *)central;

/** 扫描到新的外设 */
- (void)centerManager:(JMCenterManager *)centerManager didScanNewPeripheral:(CBPeripheral *)peripheral WithAdvertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI;

/** 连接到外设 */
- (void)centerManager:(JMCenterManager *)centerManager didconnectPeripheral:(CBPeripheral *)peripheral;

/** 与外设断开连接 */
- (void)centerManager:(JMCenterManager *)centerManager didDisconnectPeripheral:(CBPeripheral *)peripheral;

/** 可以向外设发送数据 */
- (void)centerManager:(JMCenterManager *)centerManager canSendDataWithPeripheral:(CBPeripheral *)peripheral andCharateristic:(CBCharacteristic *)characteristic;

/** 处理外设数据 */
- (void)centerManager:(JMCenterManager *)centerManager handleReceiveDatas:(NSMutableData *)receiveDatas;

/** 通知代理手机的蓝牙打开与否 */
- (void)centerManager:(JMCenterManager *)centerManager phoneBlueToothIsOpend:(BOOL)opend;
@end

@interface JMCenterManager : NSObject
/** 存放扫描到的外设 */
@property (strong, nonatomic) NSMutableArray *peripherals;

/** 中心设备管理者 */
@property (strong, nonatomic) CBCentralManager *manger;

/** 连接的外设 */
@property (strong, nonatomic) CBPeripheral *connectPeripheral;

/** 外设特征 */
@property (strong, nonatomic) CBCharacteristic *characteristic;

/** 蓝牙是否连接着 */
@property (assign, nonatomic,getter=isConnected, readonly) BOOL connected;

/** 手机蓝牙是否打开着 */
@property (assign, nonatomic,getter=isConnected, readonly) BOOL opened;

/** 是否找到本产品蓝牙 */
@property (assign, nonatomic, getter=isFind) BOOL find; // 是否扫描到XXX-8

/** 接收到的命令 */
@property (copy, nonatomic) NSString *receiveOrder;

/** 接受到的ACK值 */
@property (strong, nonatomic) NSData *receiveACK; 

@property (weak, nonatomic) id <JMCenterManagerDelegate> delegate;

+ (instancetype)sharedCenterManager;

/** 开始扫描 */
+ (void)beginScan;

/** 停止扫描 */
+ (void)stopScan;

/** 寻找服务 */
+ (void)discoveryServices;

/** 开始扫描具体外设 */
- (void)beginScan;

/** 断开与外设的连接 */
- (void)cancelConnect:(CBPeripheral *)peripheral;

/** 连接外设 */
- (void)connectPeripheral:(CBPeripheral *)peripheral;

/** 寻找服务 */
- (void)peripheral:(CBPeripheral *)peripheral discoverServices:(NSArray *)serverUUIDs;
@end
